package com.laoyao.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.laoyao.beans.PageBean;
import com.laoyao.comm.config.CrossOrigin;
import com.laoyao.comm.model.History;
import com.laoyao.comm.model.Praise;
import com.laoyao.comm.model.User;
import com.laoyao.comm.model.Video;
import com.laoyao.service.CollectService;
import com.laoyao.service.TypeService;
import com.laoyao.service.VideoService;
import com.laoyao.util.DateUtil;
import com.laoyao.util.TextUtil;
import com.laoyao.validate.VideoValidate;

@CrossOrigin
public class VideoController extends Controller {
	
	//添加视频
	@Before(VideoValidate.class)
	public void add(){
		Video v = getBean(Video.class,"");
		
		if(TextUtil.isEmpty(v.getTitle())){
			setAttr("msg","视频标题不能为空");
			renderJson();
			return;
		}
		if(TextUtil.isEmpty(v.getCoverPhoto())){
			setAttr("msg","视频封面不能为空");
			renderJson();
			return;
		}
		v.setUploadTime(DateUtil.getTime());
	
		renderJson("success",v.save());
		
	}
	//修改视频信息
		
	//获取类型
	public void getType(){
		renderJson(TypeService.me.getType());
	}
	
	//根据一级分类获取视频
	public void getVideoByType1(){
		PageBean p = getBean(PageBean.class,"");
		String id = getPara("id");
		VideoService.me.getVideoByType1(id, p.getPageNumber(), p.getPageSize());
	}
	//根据一级二级分类获取视频
	public void getVideoByType(){
		PageBean p = getBean(PageBean.class,"");
		String type1Id = getPara("type1Id");
		String type2Id = getPara("type2Id");
		renderJson("page",VideoService.me.getVideoByType(type1Id,type2Id, p.getPageNumber(), p.getPageSize()));
	}
	//根据视频id 获取视频
	public void getVideoById(){
		String id = getPara("id");
		String userId = getPara("userId");
		//播放量+1
		String add = "update v_video set playNum=playNum+1 where id = ?";
		Db.update(add,id);
		
		String sql  = "select a.*,b.typeName type1Name,c.typeName type2Name from v_video a,v_type_1 b,v_type_2 c where a.type1Id=b.id and a.type2Id=c.id and a.id=?";
		
		setAttr("video",Db.findFirst(sql,id));
		
		setAttr("collectNum",CollectService.me.collectNum(id));
		setAttr("isCollect",CollectService.me.isExist(userId, id));
		
		setAttr("praiseNum",VideoService.me.getPraiseNum(id));
		setAttr("isPraise",VideoService.me.isExistPraise(userId, id));
		
		renderJson();
	}
	
	//获取自己投稿视频
	public void getMyVideo(){
		PageBean p = getBean(PageBean.class,"");
		String id = getPara("id");
		renderJson("page",VideoService.me.getMyVideo(p.getPageNumber(), p.getPageSize(), id));
	}
	//获取我关注的人发表的视频
	public void getMyAttention(){
		PageBean p = getBean(PageBean.class,"");
		String id = getPara("id");
		renderJson("page",VideoService.me.getMyAttention(p.getPageNumber(), p.getPageSize(), id));
	}
	
	//删除自己所投稿的视频
	public void delVideo(){
		String videoId = getPara("videoId");
		String userId = getPara("userId");
		String sql = "delete from v_video where id=? and userId=?";
		renderJson("success",Db.update(sql,videoId,userId));
	}
	
	
	//单独获取视频收藏数量
	public void getCollectNum(){
		String id = getPara("id");
		renderJson("collectNum",CollectService.me.collectNum(id));
	}
	//单独获取视频点赞数量
	public void getPraiseNum(){
		String id = getPara("id");
		renderJson("praiseNum",VideoService.me.getPraiseNum(id));
	}
	//视频点赞操作
	public void praise(){
		Praise p = getBean(Praise.class,"");
		Integer flag = getParaToInt("flag");
		if(flag==0){
			renderJson("success",p.delete());
			return;
		}
		
		if(flag==1){
			try {
				renderJson("success",p.save());
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();		
			}
		}
	}
	//添加播放历史
	public void playHistory(){
		History h = getBean(History.class,"");
		if(h.getUserId()==null){
			renderJson();
			return;
		}
		h.setTime(DateUtil.getTime());
		if(VideoService.me.isExistHistory(h.getUserId(), h.getVideoId())){
			renderJson("success",h.update());
			return;
		}
		renderJson("success",h.save());	
	}
	//获取播放历史列表
	public void historyList(){
		PageBean p = getBean(PageBean.class,"");
		String id = getPara("id");	
		renderJson("page",VideoService.me.historyList(p.getPageNumber(), p.getPageSize(), id));		
	}
	//删除播放历史
	public void delHistory(){
		History h = getBean(History.class,"");
		renderJson("success",h.delete());
	}
	
	//返回整个视频列表
	public void getVideoList(){
		PageBean p = getBean(PageBean.class,"");
		Video v = getBean(Video.class,"");
		String isVip = getPara("isVip");
		String type1Id = getPara("type1Id");
		String type2Id = getPara("type2Id");
		renderJson("page",VideoService.me.list(p.getPageNumber(), p.getPageSize(), v.getTitle(),isVip,type1Id,type2Id ));	
	}
	
	//判定播放权限
	public void isPlay(){
		String userId = getPara("userId");
		String videoId = getPara("videoId");
		
		Video v = Video.dao.findById(videoId);
		if(v==null){
			return;
		}
		if(v.getIsVip()==0){
			renderJson("isPlay",true);
			return;
		}
		
		if(TextUtil.isEmpty(userId)){
			renderJson("isPlay",false);
			return;
		}
		User u  = User.dao.findById(userId);
		if(u==null||DateUtil.dateCmp(u.getVipTime(), DateUtil.getTime())==-1){
			renderJson("isPlay",false);
			return;
		}
		renderJson("isPlay",true);
		return;
	}
	
	//获取视频排行数据
	public void rankings(){
		PageBean p = getBean(PageBean.class,"");
		String type1Id = getPara("type1Id");
		String type2Id = getPara("type2Id");
		renderJson("page",VideoService.me.rankings(p.getPageNumber(), p.getPageSize(), type1Id, type2Id));
	}
	//获取近期排名前几的视频
	public void top(){
		PageBean p = getBean(PageBean.class,"");
		renderJson("page",VideoService.me.top(p.getPageNumber(), p.getPageSize()));
	}
	//相关推荐
	public void relate(){
		PageBean p = getBean(PageBean.class,"");
		String type1Id = getPara("type1Id");
		String type2Id = getPara("type2Id");
		String title = getPara("title");
		renderJson("page",VideoService.me.relate(p.getPageNumber(), p.getPageSize(),title, type1Id, type2Id));
	}
	
	//视频审核状态
	public void videoCheckState(){
		String state = getPara("state");
		String id = getPara("id");
		String sql  = "update v_video set checkState=? where id=?";
		renderJson("success",Db.update(sql,id,state));
	}

}
